package catcatcat;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class CaoZuoSql {
	String JDBC_DRIVER="com.mysql.jdbc.Driver";
	String DB_URL="jdbc:mysql://localhost:3306/miaoshe";
	String USER="root";
	String PASS="123456";
	Connection conn=null;
	Statement stmt=null;
		public CaoZuoSql(){

			try {
				Class.forName("com.mysql.jdbc.Driver");
				System.out.println("连接数据库。。。");
				conn=(Connection) DriverManager.getConnection(DB_URL,USER,PASS);
				System.out.println("实例化Statement对象。");
				stmt=(Statement) conn.createStatement();
			}catch (Exception e) {
				e.printStackTrace();
				System.out.println("实例化出现错误！");
			}
		}
		//根据name查询密码
		public String chaXunPwsd(String name){
			String sql;
			sql="select * from miaomiao where name="+"'"+name+"';";
			System.out.println(sql);
			String str = null;
			try {
				ResultSet rs=stmt.executeQuery(sql);
				while(rs.next()){
					String pwsd=rs.getString("pwsd");
					str=pwsd;
					System.out.print("pwsd="+pwsd);
					System.out.println("\n");
				}
				rs.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("查询密码出现错误！");
			}
			return str;
		}
		
		public boolean tianJia(String name,String pwsd) {
			try {
				String sql;
				sql="insert into miaomiao values('"+name+"','"+pwsd+"');";
				System.out.println(sql);
				int i=stmt.executeUpdate(sql);
				if (i>0) {
					return true;
				}
				return false;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
		}
		
		public void guanBi(){
			try {
				stmt.close();
				conn.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("关闭出现错误！");
			}
			
		}
		
}
